#!/bin/sh
#

set -e

. /etc/default/openmediavault
. /usr/share/openmediavault/scripts/helper-functions

OMV_VBOX_USER=${OMV_VBOX_USER:-"vbox"}

case "$1" in
    configure)

	# Check for XML config
    if ! omv_config_exists "//services/virtualbox"; then
		omv_config_add_element "//services" "virtualbox" ""
		omv_config_add_element "//services/virtualbox" "enable" "0"
		omv_config_add_element "//services/virtualbox" "enable-advanced" "0"
		omv_config_add_element "//services/virtualbox" "vm-folder" ""
	fi

	# Check for user config
	if [ ! -e /etc/default/openmediavault-virtualbox ]; then
		
		# No user config, should the user be created?
		if [ $(groups ${OMV_VBOX_USER} 2>/dev/null | wc -l) -eq 0 ]; then
			
			# Generate random password
			NPSWD=$(makepasswd --crypt-md5 --chars=10)
			NPSWD_CLEAR=$(echo ${NPSWD} | awk '{ print $1 }')
			NPSWD_CRYPT=$(echo ${NPSWD} | awk '{ print $2 }')
			
			# Add user
			useradd -d /home/${OMV_VBOX_USER} -c "openmediavault-virtualbox" -m -p ${NPSWD_CRYPT} -N -g vboxusers ${OMV_VBOX_USER}
			
			# Store password for vboxwebsrv connectivity
			echo "${NPSWD_CLEAR}" >/etc/default/openmediavault-virtualbox
			
		fi
		
	fi
	
	# Fix permissions
	chown root:root /etc/default/openmediavault-virtualbox
	chmod 0600 /etc/default/openmediavault-virtualbox
	chmod u+rx /etc/init.d/openmediavault-virtualbox
	
	omv_install_fixperms
	
	# Clean up old version of plugin
	if [ -f /etc/apache2/sites-enabled/phpvirtualbox ]; then
		/bin/rm -f /etc/apache2/sites-enabled/phpvirtualbox
	fi

	if [ -f /etc/apache2/sites-available/phpvirtualbox ]; then
		/bin/rm -f /etc/apache2/sites-available/phpvirtualbox
	fi
	
	# Reload apache config
	monit restart apache2

    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac


exit 0
